#include "bms_Slave.h"
#include "bms_Slave_ChrgMode_Cal.h"
#include "Alarm_Recov_sys.h"
#include "rtwtypes.h"
#include "bms_Slave_private.h"
#include "calPrms.h"

void bms_Slave_ChrgMode_Cal_Init(void)
{
  bms_Slave_DW.UnitDelay_DSTATE_a = 2U;
  bms_Slave_Alarm_Recov_sys_Init(&bms_Slave_B.Alarm_flg_ne);
}

void bms_Slave_ChrgMode_Cal_Reset(void)
{
  bms_Slave_DW.UnitDelay_DSTATE_a = 2U;
  bms_Slave_Alarm_Recov_sys_Reset(&bms_Slave_B.Alarm_flg_ne,
    &bms_Slave_DW.sf_Alarm_Recov_sys3_iu);
}

void bms_Slave_ChrgMode_Cal_Disable(void)
{
  bms_Slave_DW.ChrgMode_Cal_MODE = false;
}

void bms_Slave_ChrgMode_Cal(void)
{
  if (bms_Slave_B.LogicalOperator9) {
    boolean_T rtb_RelationalOperator;
    if (!bms_Slave_DW.ChrgMode_Cal_MODE) {
      bms_Slave_ChrgMode_Cal_Reset();
      bms_Slave_DW.ChrgMode_Cal_MODE = true;
    }

    rtb_RelationalOperator = (bms_Slave_B.CellV_max >= CellV_limit);
    bms_Slave_Alarm_Recov_sys(rtb_RelationalOperator, !rtb_RelationalOperator,
      200, 1, &bms_Slave_B.Alarm_flg_ne, &bms_Slave_DW.sf_Alarm_Recov_sys3_iu);
    if (bms_Slave_B.Alarm_flg_ne) {
      bms_Slave_B.ChrgMode = 1U;
    } else {
      bms_Slave_B.ChrgMode = bms_Slave_DW.UnitDelay_DSTATE_a;
    }

    bms_Slave_DW.UnitDelay_DSTATE_a = bms_Slave_B.ChrgMode;
  } else if (bms_Slave_DW.ChrgMode_Cal_MODE) {
    bms_Slave_ChrgMode_Cal_Disable();
  }
}
